/**
	This program computes the averages of the data contained by the file
	products in testing the application and write them into a gnuplot's
	readable format
*/
#include <stdio.h>

int main(int argc, char *argv[])
{
	/*
		MANCANo:
		- controllo errori
		- altri parametri?
		- file di test come parametro
	*/
	FILE *ftest,*fout;
	int num_pe,num_pe_act,count;
	long int tmp, cum;
	float avg;
	ftest=fopen("test.dat","r");
	fout=fopen("result.dat","a");
	fscanf(ftest,"%d %ld",&num_pe,&cum);
	num_pe_act=num_pe;
	while(!feof(ftest))
	{
		/* Sum up all the times for the same number of proc. element*/
		count=1;
		while(!feof(ftest))
		{
			fscanf(ftest,"%d %ld",&num_pe_act,&tmp);
			if(num_pe_act==num_pe)
			{
				cum+=tmp;
				count++;
				/*printf("Letto %d %ld,  cum=%d, count=%d\n",num_pe_act,tmp, cum,count);*/
			}
			else
			{
				/*compute average and print out*/
				/*printf("Average ----- Letto %d %ld,  cum=%d, count=%d\n",num_pe_act,tmp, cum,count);*/
				avg=(float)(((float)cum)/count);
				fprintf(fout,"%d %f\n",num_pe,avg);
				num_pe=num_pe_act;
				cum=tmp;
				break;
			}
		}
	}
	/*printf("Average ----- Letto %d %ld,  cum=%d, count=%d\n",num_pe_act,tmp, cum,count);*/
				avg=(float)(((float)cum)/count);
				fprintf(fout,"%d %f\n",num_pe,avg);
	fclose(ftest);
	fclose(fout);
}
